package com.kichina.parking.controller.parking.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.kichina.parking.service.logic.bean.*;
import com.kichina.parking.service.logic.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName CarBanDownContorlHandler
 * @date 2022-09-07 15:19
 * @desc App请求远程停车管理服务业务处理
 */
@Component
@Slf4j
public class AppParkingHandler {

    private final ChannelInfoRecordService channelInfoRecordService;

    private final ParkingRecordService parkingRecordService;

    private final CarTypeRecordService carTypeRecordService;

    private final LicensePlateTypeRecordService licensePlateTypeRecordService;

    private final ChargeRecordService chargeRecordService;

    private final WhitelistInfoRecordService whitelistInfoRecordService;

    private final StationaryCarRecordService stationaryCarRecordService;

    public AppParkingHandler(ChannelInfoRecordService channelInfoRecordService, ParkingRecordService parkingRecordService, CarTypeRecordService carTypeRecordService, LicensePlateTypeRecordService licensePlateTypeRecordService, ChargeRecordService chargeRecordService, WhitelistInfoRecordService whitelistInfoRecordService, StationaryCarRecordService stationaryCarRecordService) {
        this.channelInfoRecordService = channelInfoRecordService;
        this.parkingRecordService = parkingRecordService;
        this.carTypeRecordService = carTypeRecordService;
        this.licensePlateTypeRecordService = licensePlateTypeRecordService;
        this.chargeRecordService = chargeRecordService;
        this.whitelistInfoRecordService = whitelistInfoRecordService;
        this.stationaryCarRecordService = stationaryCarRecordService;
    }

    /**
     * @param parkId 车场ID
     * @author Surging
     * @Description 根据车场ID
     * @Return java.util.List<com.kichina.parking.service.logic.bean.ChannelInfoRecord>
     * @date 17:24 2022-09-20
     **/
    public List<ChannelInfoRecord> listParkingGate(String parkId) {
        List<ChannelInfoRecord> channelInfoRecords = channelInfoRecordService.list(new QueryWrapper<ChannelInfoRecord>()
                .lambda().eq(ChannelInfoRecord ::getParkid, parkId)
                .ne(ChannelInfoRecord ::getOperateType, 3));
        if (CollectionUtils.isEmpty(channelInfoRecords)){
            channelInfoRecords = Lists.newArrayList();
        }
        return channelInfoRecords;
    }

    /**
     * @param parkId 车场ID
     * @author Surging
     * @Description 获取车场车辆进出记录集合
     * @Return java.util.List<com.kichina.parking.service.logic.bean.ParkingRecord>
     * @date 15:35 2022-09-22
     **/
    public List<ParkingRecord> listCarRecord(String parkId) {
        List<ParkingRecord> parkingRecords = parkingRecordService.list(new QueryWrapper<ParkingRecord>()
                .lambda().eq(ParkingRecord ::getParkid, parkId).orderByDesc(ParkingRecord::getCreateTime));
        if (CollectionUtils.isEmpty(parkingRecords)){
            parkingRecords = Lists.newArrayList();
        }
        return parkingRecords;
    }

    /**
     * @param parkId 车场ID
     * @author Surging
     * @Description 获取车辆类型
     * @Return java.util.List<com.kichina.parking.service.logic.bean.CarTypeRecord>
     * @date 15:37 2022-09-22
     **/
    public List<CarTypeRecord> listCarType(String parkId) {
        List<CarTypeRecord> carTypeRecords = carTypeRecordService.list(new QueryWrapper<CarTypeRecord>()
                .lambda().eq(CarTypeRecord ::getParkid, parkId)
                .ne(CarTypeRecord ::getOperateType, 3));
        if (CollectionUtils.isEmpty(carTypeRecords)){
            carTypeRecords = Lists.newArrayList();
        }
        return carTypeRecords;
    }

    /**
     * @param parkId 车场ID
     * @author Surging
     * @Description 获取车牌类型
     * @Return java.util.List<com.kichina.parking.service.logic.bean.LicensePlateTypeRecord>
     * @date 15:41 2022-09-22
     **/
    public List<LicensePlateTypeRecord> listCardType(String parkId) {
        List<LicensePlateTypeRecord> licensePlateTypeRecords = licensePlateTypeRecordService.list(new QueryWrapper<LicensePlateTypeRecord>()
                .lambda().eq(LicensePlateTypeRecord ::getParkid, parkId)
                .ne(LicensePlateTypeRecord ::getOperateType, 3));
        if (CollectionUtils.isEmpty(licensePlateTypeRecords)){
            licensePlateTypeRecords = Lists.newArrayList();
        }
        return licensePlateTypeRecords;
    }

    /**
     * @param parkId 车场ID
     * @param carNumber 车牌号
     * @author Surging
     * @Description 获取固定车信息集合
     * @Return java.util.List<com.kichina.parking.service.logic.bean.WhitelistInfoRecord>
     * @date 16:00 2022-09-22
     **/
    public List<WhitelistInfoRecord> listChargeRecord(String parkId, String carNumber) {
        //一次性查出，减少遍历查询导致接口变慢
        QueryWrapper<WhitelistInfoRecord> whitelistInfoRecordQueryWrapper = new QueryWrapper<>();

        if (Objects.isNull(carNumber)){
            whitelistInfoRecordQueryWrapper
                    .lambda().eq(WhitelistInfoRecord::getParkid, parkId)
                    .eq(WhitelistInfoRecord::getDealTag, true)
                    .ne(WhitelistInfoRecord::getOperateType, 3)
                    .orderByDesc(WhitelistInfoRecord ::getServiceCreateTime);
        }else {
            // 车牌号模糊查询
            whitelistInfoRecordQueryWrapper
                    .lambda().eq(WhitelistInfoRecord::getParkid, parkId)
                    .like(WhitelistInfoRecord::getCarNumber, carNumber)
                    .eq(WhitelistInfoRecord::getDealTag, true)
                    .ne(WhitelistInfoRecord::getOperateType, 3)
                    .orderByDesc(WhitelistInfoRecord ::getServiceCreateTime);
        }
        List<WhitelistInfoRecord> whitelistInfoRecords = whitelistInfoRecordService.list(whitelistInfoRecordQueryWrapper);
        if (CollectionUtils.isEmpty(whitelistInfoRecords)){
            whitelistInfoRecords = Lists.newArrayList();
        }else {
            List<LicensePlateTypeRecord> licensePlateTypeRecords = listCardType(parkId);
            if (CollectionUtils.isEmpty(licensePlateTypeRecords)) {
                whitelistInfoRecords.forEach(whitelistInfoRecord -> whitelistInfoRecord.setCardTypeStr("充值用户"));
            }
            Map<Integer, String> cardTypeMap = new HashMap<>();
            licensePlateTypeRecords.forEach(licensePlateTypeRecord -> cardTypeMap.put(licensePlateTypeRecord.getCardType(), licensePlateTypeRecord.getCardTypedesc()));
            for(WhitelistInfoRecord whitelistInfoRecord : whitelistInfoRecords){
                if(Objects.nonNull(cardTypeMap.get(whitelistInfoRecord.getCardType()))){
                    whitelistInfoRecord.setCardTypeStr(cardTypeMap.get(whitelistInfoRecord.getCardType()));
                } else {
                    whitelistInfoRecord.setCardTypeStr("充值用户");
                }
            }
        }
        return whitelistInfoRecords;
    }

}
